﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using WhiteFlow.Core.Caching;
using WhiteFlow.DevTrackingTool.UI.Events;
using WhiteFlow.DevTrackingTool.UI.Models.Cache;
using WhiteFlow.Wpf.Common.Commands;
using WhiteFlow.Wpf.Common.Mvvm.ViewModel;

namespace WhiteFlow.DevTrackingTool.UI.ViewModels
{
    public class BackstageRecentTabViewModel : ViewModelBase
    {
        #region Constants

        /// <summary>
        /// The RecentWorksheets property name.
        /// </summary>
        public const string RecentWorksheetsPropertyName = "RecentWorksheets";

        /// <summary>
        /// The RecentLocations property name.
        /// </summary>
        public const string RecentLocationsPropertyName = "RecentLocations";

        #endregion

        #region Fields

        private ObservableCollection<RecentWorksheet> recentWorksheets;

        private ObservableCollection<RecentLocation> recentLocations;

        private ICommand queryOpenRecentCommand;

        private ICommand queryPinRecentCommand;

        private ICommand queryOpenFromRecentLocationCommand;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the recent worksheets.
        /// </summary>
        /// <value>
        /// The recent worksheets.
        /// </value>
        public ObservableCollection<RecentWorksheet> RecentWorksheets
        {
            get { return recentWorksheets; }
            set
            {
                recentWorksheets = value;
                OnPropertyChanged(RecentWorksheetsPropertyName);

                if (value != null)
                {
                    RecentLocations = new ObservableCollection<RecentLocation>(
                        (from w in value
                         select new RecentLocation(w.FullPath)).Distinct<RecentLocation>().ToList());
                }
            }
        }

        /// <summary>
        /// Gets or sets the recent locations.
        /// </summary>
        /// <value>
        /// The recent locations.
        /// </value>
        public ObservableCollection<RecentLocation> RecentLocations
        {
            get { return recentLocations; }
            set
            {
                recentLocations = value;
                OnPropertyChanged(RecentLocationsPropertyName);
            }
        }

        #endregion

        #region Commands

        public ICommand QueryOpenRecentCommand
        {
            get
            {
                if (queryOpenRecentCommand == null)
                    queryOpenRecentCommand = new DelegateCommand<RecentWorksheet>(QueryOpenRecent);

                return queryOpenRecentCommand;
            }
        }

        public ICommand QueryPinRecentCommand
        {
            get
            {
                if (queryPinRecentCommand == null)
                    queryPinRecentCommand = new DelegateCommand<RecentWorksheet>(QueryPinRecent);

                return queryPinRecentCommand;
            }
        }

        public ICommand QueryOpenFromRecentLocationCommand
        {
            get
            {
                if (queryOpenFromRecentLocationCommand == null)
                    queryOpenFromRecentLocationCommand = new DelegateCommand<string>(QueryOpenFromRecentLocation);

                return queryOpenFromRecentLocationCommand;
            }
        }

        #endregion

        #region Constructors

        public BackstageRecentTabViewModel()
        {
            LoadRecentWorksheets();
        }

        #endregion

        #region Public Methods

        public void RefreshData()
        {
            LoadRecentWorksheets();
        }

        #endregion

        #region Private Methods

        private void LoadRecentWorksheets()
        {
            DevTrackingToolCache cache = AppCacheHelper.ReadCache<DevTrackingToolCache>();
            RecentWorksheets = cache.RecentWorksheets;
        }

        private void QueryOpenRecent(RecentWorksheet recentWorksheet)
        {
            App.EventAggregator.GetEvent<QueryRecentWorksheetOpenEvent>().Publish(recentWorksheet);
        }

        private void QueryPinRecent(RecentWorksheet recentWorksheet)
        {
            // TODO...
        }

        private void QueryOpenFromRecentLocation(string recentLocation)
        {
            App.EventAggregator.GetEvent<QueryOpenWorksheetFromRecentLocationEvent>().Publish(recentLocation);
        }

        #endregion
    }
}